package com.wsocket.controller;

import com.wsocket.websocket.WebSocketConfigServer;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.WebSocketImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.net.UnknownHostException;

/**
 * @description:
 * @title: WebSocketController
 * @author: lcMeng
 * @createDate: 2021/3/20 14:32
 * @version: 1.0
 */
@Slf4j
@Controller
public class WebSocketController {

    /**
     * 启动socket服务
     */
    @PostConstruct
    public void startWebsocketOnline() {
        log.info("开始启动websocket");
        WebSocketImpl.DEBUG = false;
        // 端口随便设置，只要不跟现有端口重复就可以
        int port = 8888;
        WebSocketConfigServer s = null;
        try {
            s = new WebSocketConfigServer(port);
            s.start();
        } catch (UnknownHostException e) {
            log.error("启动websocket失败！");
            e.printStackTrace();
        }
        log.info("启动websocket成功！");
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestParam("userName") String userName) {
        log.info("username is:" + userName);
        return userName;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login1(HttpServletRequest request) {
        return "login";
    }


}
